var http = require("http");
const fs=require("fs")
const path=require("path");


http.createServer((req,res) => {

    var fileName=process.argv[2] 
    var filePath=path.join(__dirname,"/"+fileName);
    var filePath1=path.join(__dirname,"/fileReader2.js");

    if(fs.existsSync(filePath)){
        fs.open(filePath,'r',(err,data)=>{
            if(err){
                console.log(err)
            }else{
                var buf=Buffer.alloc(1024);
                fs.read(data,buf,0,buf.length,0,(err,bytesRead,buf)=>{
                 if(err) {
                     throw err;
                   } else{
                     res.end(buf.slice(0,bytesRead).toString("utf-8"));
                   }
                })
            }
         })
    }else if(fileName==undefined){
        fs.open(filePath1,'r',(err,data)=>{
            if(err){
                console.log(err)
            }else{
                var buf=Buffer.alloc(2048);
                fs.read(data,buf,0,buf.length,0,(err,bytesRead,buf)=>{
                 if(err) {
                     throw err;
                   } else{
                     res.end(buf.slice(0,bytesRead).toString("utf-8"));
                   }
                })
            }
        })
    }
}).listen(8081);
console.log("server is listening 8081");










